package com.boarsoft.boar.app.action;

import java.util.ArrayList;
import java.util.List;

import com.boarsoft.boar.app.AppInstBiz;
import com.boarsoft.boar.entity.AppInst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.app.AppZoneSlotBiz;
import com.boarsoft.boar.entity.AppZoneSlot;
import com.boarsoft.common.Util;

@RestController
@RequestMapping("/app/zone/slot")
public class AppZoneSlotAction {
	private static final Logger log = LoggerFactory.getLogger(AppZoneSlotAction.class);

	@Autowired
	private AppZoneSlotBiz appZoneSlotBiz;

	@Autowired
	private AppInstBiz appInstBiz;


	@RequestMapping("/init.do")
	// @Authorized(code = "app.inst.stat")
	public ReplyInfo<Object> init(String appId, String cluster, String zones, String env, int count) {
		log.warn("Init app_zone_slot for {}/{}/{}/{}/{}", appId, cluster, zones, env, count);
		appZoneSlotBiz.deleteApp(appId);
		String[] za = zones.split(",");
		List<AppZoneSlot> lt = new ArrayList<>();
		int n = 0;
		for (int i = 0; i < count; i++) {
			for (String z : za) {
				AppZoneSlot o = new AppZoneSlot();
				o.setAppId(appId);
				o.setCluster(cluster);
				o.setEnv(env);
				o.setNo(n++);
				o.setTime(Util.getStdmfDateTime());
				o.setZone(z);
				lt.add(o);
			}
		}
		appZoneSlotBiz.save(lt);
		return ReplyInfo.SUCCESS;
	}

	@RequestMapping("/expand.do")
	public synchronized ReplyInfo<Object> expand(String cluster, String appId, String zones, String env, int count) {
		log.warn("expand app_zone_slot for {}/{}/{}/{}/{}", appId, cluster, zones, env, count);
		String[] za = zones.split(",");
		List<AppZoneSlot> lt = new ArrayList<>();
		for (String z : za) {
			long curcount=appZoneSlotBiz.selectCount(appId,cluster,z,env);
			if(count>curcount)
			for (int i = 0; i < count-curcount; i++) {
				int n = appZoneSlotBiz.selectMaxNo(appId,cluster,z,env);
				AppZoneSlot o = new AppZoneSlot();
				o.setAppId(appId);
				o.setCluster(cluster);
				o.setEnv(env);
				o.setNo(++n);
				o.setTime(Util.getStdmfDateTime());
				o.setZone(z);
				lt.add(o);
			}

		}
		appZoneSlotBiz.save(lt);
		return ReplyInfo.SUCCESS;
	}


	@PostMapping("/contract.do")
	public ReplyInfo<Object> contract(@RequestBody List<AppInst> appInsts) {
		log.warn("contract app_Insts {}", appInsts);
		List<AppZoneSlot> appZoneSlotsDel=new ArrayList<>();
		for (AppInst appInst : appInsts) {
			List<AppInst> appInstList=appInstBiz.list(appInst.getIp());
			for(AppInst a:appInstList){
				AppZoneSlot tmp=new AppZoneSlot();
				tmp.setAppId(appInst.getAppId());
				tmp.setZone(appInst.getZone());
				tmp.setEnv(appInst.getEnv());
				tmp.setCluster(appInst.getCluster());
				tmp.setAppInstId(a.getId());
				appZoneSlotsDel.add(tmp);
			}
		}
		appZoneSlotBiz.delete(appZoneSlotsDel);
		return ReplyInfo.SUCCESS;
	}
}